//
//  BaseInfoViewController.m
//  fitness-iOS
//
//  Created by 卢仕彤 on 2018/1/4.
//  Copyright © 2018年 卢仕彤. All rights reserved.
//

#import "BaseInfoViewController.h"
#import "BaseInfoViewMode.h"
#import "ModInfoViewController.h"
#import "BCSheetPickerView.h"
#import "FFDataMode.h"
#import "FFNetworkHelper.h"
#import "FFDataMode.h"

#define kBaseHeadCell @"BaseHeadCell"
#define kBaseDeatilsCell @"BaseDeatilsCell"

@interface BaseInfoViewController () <ModInfoViewCDelegate>{
    NSMutableArray *_sections;
}
@property (nonatomic, strong) NSMutableArray *dataSourceMAttay;
@property (nonatomic, strong) NSMutableArray *dataRightArray;
@end

@implementation BaseInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"基础信息";
    [self registCell];
    self.tableView.canReload = YES;
    _dataSourceMAttay = @[@"昵称",@"性别",@"生日",@"城市",@"行业",@"收入水平",@"学历",@"婚姻",@"签名"].mutableCopy;
    _dataRightArray = [[NSMutableArray alloc] init];
}

#pragma -mark ------------------tableView delegate----------------------

- (void)registCell {
    [self.tableView registerClass:[BaseHeadCell class] forCellReuseIdentifier:kBaseHeadCell];
    [self.tableView registerClass:[BaseDeatilsCell class] forCellReuseIdentifier:kBaseDeatilsCell];
}

- (NSArray *)customSections {
    if (_sections == nil) {
        _sections = [NSMutableArray array];
        // 头部的cell主要包括上面的webView
        MCUTableCustomSection *sec = [MCUTableCustomSection createWithTarget:self];
        sec = [MCUTableCustomSection createWithTarget:self];
        sec.cellForRowAtIndexSelector = @selector(cellViewForBaseheadCell:);
        sec.heightForCellSelector = @selector(heightCellForBaseheadCell:);
        sec.cellSelectedSelector = @selector(selectedForBaseheadCell:);
        [_sections addObject:sec];
        
        sec = [MCUTableCustomSection createWithTarget:self];
        sec.numberOfRowsSelector = @selector(numberOfRowForBaseBottomCell:);
        sec.cellForRowAtIndexSelector = @selector(cellViewForBaseBottomCell:);
        sec.heightForCellSelector = @selector(heightCellForBaseBottomCell:);
        sec.cellSelectedSelector = @selector(selectCellForBaseBottomCell:);
        [_sections addObject:sec];
    }
    return _sections;
}

- (NSNumber *)heightCellForBaseheadCell:(NSIndexPath *)indexPath {
    return  @125;
}

- (void)selectedForBaseheadCell:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCell *)cellViewForBaseheadCell:(NSIndexPath *)indexPath {
    BaseHeadCell *cell = [[BaseHeadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kBaseHeadCell];
    return cell;
}

- (NSNumber *)numberOfRowForBaseBottomCell:(NSIndexPath *)indexPath {
    return  @(_dataSourceMAttay.count);
}

- (NSNumber *)heightCellForBaseBottomCell:(NSIndexPath *)indexPath {
    return  @50;
}

- (void)selectCellForBaseBottomCell:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        ModInfoViewController *mVC = [[ModInfoViewController alloc] init];
        mVC.type = modUserName;
        mVC.delegate = self;
        mVC.cellString = _mode.nickname;
        [self.navigationController pushViewController:mVC animated:YES];
    }
    if (indexPath.row == 1) {
        [BCSheetPickerView BCSheetStringPickerWithTitle:@[@"男",@"女"] andHeadTitle:@"" andSelectNumber:0 andSheetstyle:SINGLEPICK Andcall:^(BCSheetPickerView *pickerView, NSString *choiceString) {
            _dataRightArray[indexPath.row] = choiceString;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
           
        }];
    }
    if (indexPath.row == 2) {
        [BCSheetPickerView BCSheetStringPickerWithTitle:@[@"男"] andHeadTitle:@"" andSelectNumber:0 andSheetstyle:DATA_PICKER Andcall:^(BCSheetPickerView *pickerView, NSString *choiceString) {
            _dataRightArray[indexPath.row] = choiceString;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }];
    }
    if (indexPath.row == 3) {
        [AddressPickerView showAddressWithConpletion:^(NSString *provinceString, NSString *city, NSString *areas) {
            _dataRightArray[indexPath.row] = [NSString stringWithFormat:@"%@ %@ %@",provinceString,city,areas];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }];
    }
    if (indexPath.row == 6) {
        [BCSheetPickerView BCSheetStringPickerWithTitle:@[@"高中",@"大专",@"本科",@"硕士",@"博士"] andHeadTitle:@"" andSelectNumber:2 andSheetstyle:SINGLEPICK Andcall:^(BCSheetPickerView *pickerView, NSString *choiceString) {
            _dataRightArray[indexPath.row] = choiceString;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }];
    }
    
    if (indexPath.row == 7) {
        [BCSheetPickerView BCSheetStringPickerWithTitle:@[@"未婚",@"已婚"] andHeadTitle:@"" andSelectNumber:0 andSheetstyle:SINGLEPICK Andcall:^(BCSheetPickerView *pickerView, NSString *choiceString) {
            _dataRightArray[indexPath.row] = choiceString;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }];
    }
    
}

- (UITableViewCell *)cellViewForBaseBottomCell:(NSIndexPath *)indexPath {
    BaseDeatilsCell *cell = [[BaseDeatilsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kBaseDeatilsCell];
    cell.leftBaseLabel.text = _dataSourceMAttay[indexPath.row];
    cell.rightBaseLabel.text = [self getRightText][indexPath.row];
    return cell;
}

- (NSMutableArray *) getRightText {
    if (_dataRightArray.count == 0) {
        [_dataRightArray addObject:_mode.nickname];
        if ([_mode.sex integerValue] == 0 ) {
            [_dataRightArray addObject:@"男"];
        }
        [_dataRightArray addObject:_mode.birthday];
        [_dataRightArray addObject:_mode.city];
        [_dataRightArray addObject:_mode.industry];
        [_dataRightArray addObject:_mode.income];
        [_dataRightArray addObject:_mode.education];
        [_dataRightArray addObject:_mode.marriage];
        [_dataRightArray addObject:_mode.signature];
        [_dataRightArray addObject:@"女"];
    }
    return _dataRightArray;
}

- (void)show_ModInfoViewC:(ModInfoViewController *)modInfoViewC dic:(NSMutableDictionary *)dic {
    NSLog(@"%@",dic);
    if (dic[@"nickName"]) {
        _dataRightArray[0] = dic[@"nickName"];
        [self.tableView reloadData];
    }
}

- (void)returnClick {
    NSUInteger sex = 1;
    if ([_dataRightArray[1] isEqualToString:@"女"]) {
        sex = 2;
    }
    NSDictionary *dic = @{@"nickname":_dataRightArray[0],
                          @"sex":@(sex),
                          @"birthday":_dataRightArray[2],
                          @"education":_dataRightArray[6],
                          @"marriage":_dataRightArray[7],
                          @"user_id":[FFDataMode shareCenter].myInfoModel.user_id
                          };
    NSDictionary *parmsDic = @{@"userModel":dic};
    
    [FFNetworkHelper openLog];
    
    [FFNetworkHelper POST:@"user/update" parameters:parmsDic success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            [[FFDataMode shareCenter] clearData];
            [[FFDataMode shareCenter] get_myInfoModelAPI:nil];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSError *error) {
        
    }];
    
}


@end
